package com.eonmind.pay.config;

import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.ScheduledUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;

/**
 * @ClassName WxPayConfig
 * @Description TODO
 * Author 32004
 * Date 2025/4/29 15:08
 * Version 1.0
 **/

@Configuration
@ConfigurationProperties(prefix = "wxpay")
@Data
@Slf4j
public class WxPayConfig {

    // 商户号
    private String mchId;

    // 商户API证书序列号
    private String mchSerialNo;

    // 商户APIv3密钥
    private String apiV3Key;

    //APPID
    private String appId;

    //接收结果通知地址
    private String notifyDomain;

    // 商户私钥文件地址
    private String privateKeyPath;

    //微信支付地址
    private String domain;

    /*
     * 获取商户的私钥文件
     */
    private PrivateKey getPrivateKey(String fileName) {
        log.info("商户私钥文件路径:{}", fileName);
        PrivateKey merchantPrivateKey = null;
        merchantPrivateKey = PemUtil.loadPrivateKey(
                privateKeyPath
        );
        return merchantPrivateKey;
    }

    /**
     * @Author 32004
     * @Description 获取签名验证器
     * @Date 15:32 2025/4/29
     * @Param []
     * @return com.wechat.pay.contrib.apache.httpclient.auth.ScheduledUpdateCertificatesVerifier
     **/
    @Bean
    public ScheduledUpdateCertificatesVerifier getVerifier(){
        //获取商户私钥
        PrivateKey privateKey = getPrivateKey(privateKeyPath);

        //私钥签名对象
        PrivateKeySigner signer = new PrivateKeySigner(mchSerialNo, privateKey);
        WechatPay2Credentials wechatPay2Credentials = new WechatPay2Credentials(mchId, signer);

        ScheduledUpdateCertificatesVerifier scheduledUpdateCertificatesVerifier = new ScheduledUpdateCertificatesVerifier(
                wechatPay2Credentials,
                apiV3Key.getBytes(StandardCharsets.UTF_8));
        return scheduledUpdateCertificatesVerifier;
    }

    /**
     * @Author 32004
     * @Description 获取微信支付请求对象
     * @Date 15:32 2025/4/29
     * @Param [verifier]
     * @return org.apache.http.impl.client.CloseableHttpClient
     **/
    @Bean
    public CloseableHttpClient getWxPayClient( ScheduledUpdateCertificatesVerifier verifier){
        // 商户私钥
        PrivateKey privateKey = getPrivateKey(privateKeyPath);

        //使用定时更新的签名验证器，不需要传入证书
        WechatPayHttpClientBuilder builder= WechatPayHttpClientBuilder.create()
                .withMerchant(mchId, mchSerialNo, privateKey)
                .withValidator(new WechatPay2Validator(verifier));

        //通过WechatPayHttpClientBuilder创建httpClient对象，会自动处理签名和验签，并进行证书自动更新
        CloseableHttpClient httpClient=builder.build();
        return httpClient;
    }

}
